package com.gcb.controller;

import com.gcb.constant.MessageConstant;
import com.gcb.constant.RedisConstant;
import com.gcb.entity.PageResult;
import com.gcb.entity.QueryPageBean;
import com.gcb.entity.Result;
import com.gcb.pojo.CheckGroup;
import com.gcb.pojo.Setmeal;
import com.gcb.service.SetMealService;
import com.gcb.util.QiniuUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
 * @Author 35235
 * @Date 2021/8/6 12:47
 * @Version 1.0
 */
@RestController
@RequestMapping("/setmeal")
public class SetMealController {
    @DubboReference
    SetMealService setMealService;

    @Autowired
    private JedisPool jedisPool;

    @RequestMapping("/findPage.do")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        System.out.println("----已进入查询页面----");
        PageResult pageResult = setMealService.queryQuery(queryPageBean);
        System.out.println(queryPageBean);
        return pageResult;
    }

    @RequestMapping("/findAll.do")
    public Result findAllChecgroup() {
        List<CheckGroup> checkGroups;
        try {
            checkGroups = setMealService.findAllCheckgroup();
        } catch (Exception e) {

            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroups);
    }

    @RequestMapping("/add.do")
    public Result add(Integer[] checkgroupIds, @RequestBody Setmeal setmeal) {
        boolean result = setMealService.add(checkgroupIds, setmeal);
        if (false == result) {
            return new Result(result, MessageConstant.ADD_SETMEAL_FAIL);
        } else {
            return new Result(result, MessageConstant.ADD_SETMEAL_SUCCESS);
        }
    }

    @RequestMapping("/edit.do")
    public Result update(Integer[] checkgroupIds, @RequestBody Setmeal setmeal) {
        boolean result = setMealService.update(checkgroupIds, setmeal);
        //将图片名称保存到Redis集合中
        String fileName = setmeal.getImg();
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,fileName);
        if (false == result) {
            return new Result(result, "编辑套餐失败");
        } else {
            return new Result(result, "编辑套餐成功");
        }
    }

    @RequestMapping("/delete.do")
    public Result delete(Integer id) {
        boolean result = setMealService.delete(id);
        if (false == result) {
            return new Result(false, "删除套餐失败");
        } else {
            return new Result(true, "删除套餐成功");
        }
    }

    @RequestMapping("/findcheckgroupIdsBySetmealId.do")
    public Result findcheckgroupIdsBySetmealId(Integer id) {
        System.out.println("已进入查找ids");
        Integer[] checkgroupIds;
        try {
            checkgroupIds = setMealService.findCheckGroupIdsBySetMealId(id);
            System.out.println(Arrays.toString(checkgroupIds));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkgroupIds);
    }

    @RequestMapping("/findById.do")
    public Result findById(Integer id) {
        Setmeal setmeal;

        try {
            System.out.println(setmeal = setMealService.findById(id));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }
        return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, setmeal);
    }

    //图片上传
    @RequestMapping("/upload.do")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile) {
        try {//获取原始文件名
            String originalFilename = imgFile.getOriginalFilename();
            int lastIndexOf = originalFilename.lastIndexOf(".");
            //获取文件后缀
            String suffix = originalFilename.substring(lastIndexOf - 1);
            //使用UUID随机产生文件名称，防止同名文件覆盖
            String fileName = UUID.randomUUID().toString() + suffix;
            // 上传到七牛云
            QiniuUtils.upload2Qiniu(imgFile.getBytes(), fileName);
            //图片上传成功
            Result result = new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS);
            // 将上传图片名称存入 Redis，基于 Redis 的 Set 集合存储
            final Long sadd = jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);
            System.out.println("sadd = " + sadd);
            result.setData(fileName);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            //图⽚上传失败
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
    }
}
